package controllers;


import com.google.gson.JsonObject;
import constants.TplPath;
import constants.system.MemberShip;
import constants.ws.PageBean;
import duppentity.dto.merchant.SysAdminDto;
import duppentity.dto.merchant.SysAdminDtoUtil;
import duppentity.dto.sys.SysHelpDto;
import duppentity.dto.sys.SysHelpDtoUtil;
import utils.RequestHelper;
import utils.Utils;

import java.util.ArrayList;
import java.util.List;

import static play.libs.Codec.hexMD5;

/**
 * @author yangzhi
 * @date 2019/1/14
 * @desc SACC智能车美账号管理
 */
public class SysController extends ApiController{

	/**
	 * SACC智能车美账号管理
	 */
	public static void userList(){
        JsonObject data = stationDupp.getStationList(null,null, 0, 10);
        int total = data.get("total").getAsInt();
		String page = TplPath.userList;
		if (Utils.exitFile(page)){
			renderTemplate(page, total);
		}
	}

    public static void getUserList() {
        PageBean pageBean = PageBean.get();
        String name = params.get("name", String.class);
        String mobile = params.get("mobile", String.class);
        Integer type = params.get("type", Integer.class);
        Long stationId = params.get("stationId", Long.class);
        String stationName = params.get("stationName", String.class);

        JsonObject data = userDupp.getUserList(type, name, mobile, stationId, stationName, pageBean.getOffset(), pageBean.pageCount());

        if (!checkResult(data)) {
            apiError(data);
        }
        List<SysAdminDto> sysAdminList = SysAdminDtoUtil.getSysAdminDtoList(data, false);
        apiList(data, sysAdminList);
    }

    public static void addUser() {

        JsonObject roleList = roleDupp.getRoleList(0, Integer.MAX_VALUE);

        String page = TplPath.addUser;
        if (Utils.exitFile(page)) {
            renderTemplate(page, roleList);
        }
    }

    public static void editUser() {
        String page = TplPath.editUser;
        Long id = params.get("id", Long.class);
        JsonObject roleList = roleDupp.getRoleList(0, Integer.MAX_VALUE);
        JsonObject user = null;
        if (id > 0) {
            user = userDupp.getUser(id);
        }
        if (user != null) {
            SysAdminDto sysAdminDto = SysAdminDtoUtil.getSysAdminDto(user, true);
            if (Utils.exitFile(page)) {
                renderTemplate(page, sysAdminDto, roleList);
            }
        }
    }

    public static void saveUser() {
        Integer type = params.get("type", Integer.class);
        String account = params.get("account", String.class);
        String pw = params.get("pw", String.class);
        String name = params.get("name", String.class);
        String mobile = params.get("mobile", String.class);
        Long roleId = params.get("roleId", Long.class);
        String[] stationIds = params.get("stationId", String.class).split(",");
        List<Long> stationList = new ArrayList<>();
        for(String stationId : stationIds){
            if(!Utils.isNullOrEmpty(stationId)) {
                stationList.add(Long.parseLong(stationId));
            }
        }
        Long id = RequestHelper.getLongParam("id");

        if (id > 0) {
            JsonObject data = userDupp.updateUser(id, name, roleId, stationList, MemberShip.current().getId());
            if (checkResult(data)) {
                if (data.get("result").getAsBoolean()) {
                    apiOk("修改账号成功");
                } else {
                    apiError("修改账号失败");
                }
            } else {
                apiError(data);
            }
        } else {
            JsonObject data = userDupp.createUser(type, account, hexMD5(pw), mobile, name, roleId, stationList, MemberShip.current().getId());
            if (checkResult(data)) {
                if (data.get("id").getAsLong() > 0) {
                    apiOk("新增账号成功");
                } else {
                    apiError("新增账号失败");
                }
            } else {
                apiError(data);
            }
        }
    }


    public static void deleteUser() {
        Long id = params.get("id", Long.class);

        if (id.equals(MemberShip.current().getId())) {
            apiError("不能删除当前登陆账号");
        }
        JsonObject data = userDupp.deleteUser(id, MemberShip.current().getId());
        if (checkResult(data)) {
            if (data.get("result").getAsBoolean()) {
                apiOk("删除账号成功");
            } else {
                apiError("删除账号失败");
            }
        } else {
            apiError(data);
        }
    }

    /**
     * 删除用户账号和站点的关联
     */
    public static void deleteAdminStation() {
        String adminStationId = params.get("adminStationId", String.class);

        JsonObject data = userDupp.deleteAdminStation(Long.parseLong(adminStationId), MemberShip.current().getId());
        if (checkResult(data)) {
            if (data.get("result").getAsBoolean()) {
                apiOk("删除关联站点成功");
            } else {
                apiError("删除关联站点失败");
            }
        } else {
            apiError(data);
        }
    }

    /**
     * 用户反馈
     */
    public static void feedbackList(){
        String page = TplPath.feedbackList;
        if (Utils.exitFile(page)){
            renderTemplate(page);
        }
    }

    public static void getFeedbackList() {
        Long userId = params.get("userId", Long.class);
        String startCreateTime = params.get("startCreateTime", String.class);
        String endCreateTime = params.get("endCreateTime", String.class);
        PageBean pageBean = PageBean.get();

        JsonObject data = sysDupp.getSysFeedbackList(userId, startCreateTime, endCreateTime, pageBean.getOffset(), pageBean.pageCount());

        if (checkResult(data)) {
            apiOk(data);
        } else {
            apiError(data);
        }
    }

    public static void editFeedback() {
        Integer id = params.get("id", Integer.class);
        JsonObject feedback = null;

        if (id > 0) {
            feedback = sysDupp.getSysFeedback(id);
        }

        if (feedback != null) {
            String page = TplPath.editFeedback;
            if (Utils.exitFile(page)) {
                renderTemplate(page, feedback);
            }
        }
    }

    public static void replyFeedback() {
        String reply = params.get("reply", String.class);
        Integer id = params.get("id", Integer.class);

        if (id > 0) {
            JsonObject data = sysDupp.replySysFeedback(id, reply, MemberShip.current().getId());
            if (checkResult(data)) {
                if (data.get("result").getAsBoolean()) {
                    apiOk("回复成功");
                } else {
                    apiError("回复失败");
                }
            } else {
                apiError(data);
            }
        }
    }

    /**
     * 用户反馈
     */
    public static void helpList(){
        String page = TplPath.helpList;
        if (Utils.exitFile(page)){
            renderTemplate(page);
        }
    }

    public static void getHelpList() {
        PageBean pageBean = PageBean.get();
        Integer type = params.get("type", Integer.class);
        String orderBy = "sys_help.order_index desc,sys_help.create_time desc";

        JsonObject data = sysDupp.getSysHelpList(type, pageBean.getOffset(), pageBean.pageCount(), orderBy);
        if (!checkResult(data)) {
            apiError(data);
        }
        List<SysHelpDto> sysHelpList = SysHelpDtoUtil.getSysHelpDtoList(data);
        apiList(data, sysHelpList);
    }

    public static void addHelp() {
        String page = TplPath.addHelp;
        if (Utils.exitFile(page)) {
            renderTemplate(page);
        }
    }

    public static void editHelp() {
        Integer id = params.get("id", Integer.class);
        JsonObject help = null;

        if (id > 0) {
            help = sysDupp.getSysHelp(id);
        }

        if (help != null) {
            String page = TplPath.editHelp;
            if (Utils.exitFile(page)) {
                renderTemplate(page, help);
            }
        }
    }

    public static void saveHelp() {
        String title = params.get("title", String.class);
        String content = params.get("content", String.class);
        Integer visual = params.get("visual", Integer.class);
        Integer orderIndex = params.get("orderIndex", Integer.class);
        Integer type = params.get("type", Integer.class);
        Integer id = RequestHelper.getIntParam("id");

        if (id > 0) {
            JsonObject data = sysDupp.updateSysHelp(id, title, content, visual, orderIndex, type, MemberShip.current().getId());
            if (checkResult(data)) {
                if (data.get("result").getAsBoolean()) {
                    apiOk("修改问题成功");
                } else {
                    apiError("修改问题失败");
                }
            } else {
                apiError(data);
            }
        } else {
            JsonObject data = sysDupp.saveSysHelp(title, content, visual, orderIndex, type, MemberShip.current().getId());
            if (checkResult(data)) {
                if (data.get("id").getAsInt() > 0) {
                    apiOk("新增问题成功");
                } else {
                    apiError("新增问题失败");
                }
            } else {
                apiError(data);
            }
        }
    }

    public static void deleteHelp() {
        Integer id = params.get("id", Integer.class);

        JsonObject data = sysDupp.deleteSysHelp(id, MemberShip.current().getId());
        if (checkResult(data)) {
            if (data.get("result").getAsBoolean()) {
                apiOk("删除问题成功");
            } else {
                apiError("删除问题失败");
            }
        } else {
            apiError(data);
        }
    }
}
